<?php
namespace app\common\model;

use think\Model;
use think\Db;
class OrderModel extends Model {
    // 完整数据表名
    protected $table              = 'goods_order';
    // 自动时间戳
    protected $autoWriteTimestamp = true;
    // 时间输出格式
    protected $type = [
        'order_create_time' => 'timestamp',
    ];

    /*商户订单列表
    *@where 查询订单的条件（数组）  page_number 一页多少调数据的
    * @author houchengyin
    * @time 20170628
    */
    public static function order_sel($where="", $page_number='1'){
		
        $order = OrderModel::alias('order')
                        ->join('goods_order_pay gop','order.order_id = gop.order_id','left')
                        ->join('goods g', 'order.order_goods_id = g.goods_id','left')
                        ->join('goods_order_time got', 'order.order_id = got.order_id','left')
                        ->join('user u', 'order.order_user_id = u.user_id','left')
                        ->where($where)
                        ->order('got.order_generation_time desc')
                        ->paginate($page_number, false, array('query' => array('type' => input('get.type'))) );
		if(!$order){
            return false;
        }
        return $order;
    }

    /*查询商户订单详细信息
   *@where 查询订单的条件（数组）
   * @author houchengyin
   * @time 20170628
   */
    public static function order_get($where=""){
        $order = OrderModel::alias('order')
                                ->join('goods_order_time got', 'order.order_id = got.order_id','left')
                                ->join('goods_order_deliver gorf', 'order.order_id = gorf.order_id','left')
                                ->join('goods_order_rent gort', 'order.order_id = gort.order_id','left')
                                ->join('goods g', 'order.order_goods_id = g.goods_id','left')
                                ->join('goods_order_receipt gor', 'order.order_id = gor.order_id','left')
                                ->join('user u', 'order.order_user_id = u.user_id','left')
                                ->join('since se','gorf.order_since_id=se.since_id','left')
                                ->where($where)
                                ->find();
        if(!$order){
            return false;
        }
        return $order;
    }

    /*修改订单支付状态
    *@order_id修改的订单编号
    * @author houchengyin
    * @time 20170628
    */
    public static function order_status_edit($order_id){
        $order = OrderModel::update([
            'order_status'    => '2',
            'order_shipping_status' => '1'
        ],['order_id' => $order_id]);
        if(!$order){
            return false;
        }
        return true;
    }

    /*
     * 查询订单数量
     * @param array $where 查询条件
     */
    public static function order_count( $where = '' ) {
        $count = self::alias('order')
                            ->join('goods_order_pay gop','order.order_id = gop.order_id','left')
                            ->where($where)
                            ->count();
        return $count;
    }

    /*修改订单物流状态
    *@order_id修改的订单编号  data 订单物流信息
    * @author houchengyin
    * @time 20170628
    */
    public static function order_shipping_edit($order_id, $data=""){
        $order = OrderModel::where('order_id', 'eq', $order_id)->update($data);
        if(!$order){
            return false;
        }
        return true;
    }
	//结算账单Excel导出
	public static function excelBill($year,$may)
	{
		$year = input('get.year');
        $may = input('get.may');
	    $starttime = date('Y-m-01 00:00:00', strtotime($year."-".$may.'-1'));
        $starttime = date('Y-m-01 00:00:00', strtotime("$starttime +1 month"));
        $endtime = date('Y-m-d 23:59:59', strtotime("$starttime +2 month -1 day"));
        $start = strtotime($starttime);
        $end = strtotime($endtime);
        $merchant_id = session('merchant.merchant_id');
        $data= Db::table('zj_merchant_bill')
		
		->where('merchant_id', $merchant_id)->where(function ($query) use($year,$may){
                             $query->where([
                                'delete_time'=>['eq',0],
                                 'year' => ['eq',$year],
                                 'month' => ['eq',$may]
                            ]);
                        })->select();
	    $billdata=[];
		$countData=count($data);
		 
		for($i=0;$i<$countData;$i++)
		{
			 $billdata[$i]['order_sn']=html_entity_decode(" ".$data[$i]['order_sn']);;
			 //$billdata[$i]['delivery_time']=$data[$i]['delivery_time']>0?date("Y-m-d",$data[$i]['delivery_time']):'';
			 $billdata[$i]['goods_name']=$data[$i]['goods_name'];
			 //$billdata[$i]['rent_time']=$data[$i]['rent_time']>0?date("Y-m-d",$data[$i]['rent_time']):'';
			 //$billdata[$i]['return_time']=$data[$i]['return_time']>0?date("Y-m-d",$data[$i]['return_time']):'';
			 //$billdata[$i]['confirm_time']=$data[$i]['confirm_time']>0?date("Y-m-d",$data[$i]['confirm_time']):'';
            $order_status="";
            switch($data[$i]['order_status'])
            {
                case '0':
                    $order_status='退款订单';
                    break;
                case '1':
                    $order_status='成功订单';
                    break;
                case '2':
                    $order_status='待结算';
                    break;
                case '3':
                    $order_status='退货订单';
                    break;
                default:
                    break;
            }
             $billdata[$i]['order_status']=$order_status;
			 $billdata[$i]['rent_money']=sprintf("%.4f", $data[$i]['rent_money']);
            $billdata[$i]['freight_money']=sprintf("%.4f", $data[$i]['freight_money']);
            $billdata[$i]['liquidated_money']=sprintf("%.4f", $data[$i]['liquidated_money']);
			 $billdata[$i]['damages_money']=sprintf("%.4f", $data[$i]['damages_money']); 

			 //$billdata[$i]['rate']=sprintf("%.2f", $data[$i]['rate']).'%';
			 $billdata[$i]['commission']=sprintf("%.4f", $data[$i]['commission']); 
             $billdata[$i]['total_money']=sprintf("%.4f", $data[$i]['total_money']);
		}
	    return $billdata;
	 
	 
	 }
	
	
	
	

}